package is.billsharing.app;


import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;
import is.billsharing.app.*;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.json.*;

public class HomeActivity extends Activity

{
	private static final String PREFS_NAME = "user";

	public void onCreate(Bundle savedInstanceState) 
	
	{
		TableLayout user_home;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.hometest);
        user_home=(TableLayout)findViewById(R.id.user_list);
        TableRow row;
        TextView t1, t2,t3;
        String response = null;
      int i;
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        int uid = settings.getInt("uid",0);
        String uname = settings.getString("uname","noname");
       TextView  tv = (TextView)findViewById(R.id.uname_home);
      
       
       
        try {
        	ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        	postParameters.add(new BasicNameValuePair("uid", ""+uid+""));
        	response = CustomHttpClient.executeHttpPost("Home", postParameters);
        	int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(float) 1, getResources().getDisplayMetrics());
        	row = new TableRow(this);
        	t1 = new TextView(this);
        	t2 = new TextView(this);
        	t3 = new TextView(this);
        	
        	t1.setTextColor(Color.parseColor("#FFFFFF"));
        	t2.setTextColor(Color.parseColor("#FFFFFF"));
        	t3.setTextColor(Color.parseColor("#FFFFFF"));

        	t1.setText("Friend Email");
        	t2.setText("Owes You");
        	t3.setText("You Owe");
        	t1.setTypeface(null, 1);
        	t2.setTypeface(null, 1);
        	t3.setTypeface(null, 1);
        	t1.setTextSize(15);
        	t2.setTextSize(15);
        	t3.setTextSize(15);
        	t1.setWidth(200 * dip);
        	t2.setWidth(125 * dip);
        	t3.setWidth(125 * dip);
        	
        	t2.setPadding(20*dip, 0, 0, 0);
        	t3.setPadding(20*dip, 0, 0, 0);
        	row.addView(t1);
        	row.addView(t2);
        	row.addView(t3);
        	user_home.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        	
        	row = new TableRow(this);
        	t1 = new TextView(this);
        	t2 = new TextView(this);
        	t3 = new TextView(this);
        	
        	t1.setTextColor(Color.parseColor("#DA70D6"));
        	t2.setTextColor(Color.parseColor("#DA70D6"));
        	t3.setTextColor(Color.parseColor("#DA70D6"));

        	t1.setText("");
        	t2.setText("");
        	t3.setText("");
        	t1.setTypeface(null, 1);
        	t2.setTypeface(null, 1);
        	t3.setTypeface(null, 1);
        	t1.setTextSize(15);
        	t2.setTextSize(15);
        	t3.setTextSize(15);
        	t1.setWidth(200 * dip);
        	t2.setWidth(125 * dip);
        	t3.setWidth(125 * dip);
        	
        	t2.setPadding(20*dip, 0, 0, 0);
        	t3.setPadding(20*dip, 0, 0, 0);
        	row.addView(t1);
        	row.addView(t2);
        	row.addView(t3);
        	user_home.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        	
        	JSONArray jarray = new JSONArray(response);
        	for(i=0;i<jarray.length();i++)
        	{
        	JSONObject jobj=jarray.getJSONObject(i);
        
        	tv.setText(uname);
        	
        	row = new TableRow(this);
        	t1 = new TextView(this);
        	t2 = new TextView(this);
        	t3 = new TextView(this);
      	
        	t1.setTextColor(Color.parseColor("#000000"));
        	t2.setTextColor(Color.parseColor("#000000"));
        	t3.setTextColor(Color.parseColor("#000000"));
        	
        	
        	t1.setText(jobj.getString("email"));
        	t2.setText(jobj.getString("credit"));
        	t3.setText(jobj.getString("debit"));
        	t1.setTypeface(null, 1);
        	t2.setTypeface(null, 1);
        	t3.setTypeface(null, 1);
        	t1.setTextSize(15);
        	t2.setTextSize(15);
        	t3.setTextSize(15);
        	t1.setWidth(200 * dip);
        	t2.setWidth(125 * dip);
        	t3.setWidth(125 * dip);
        	
        	t2.setPadding(20*dip, 0, 0, 0);
        	t3.setPadding(20*dip, 0, 0, 0);
        	row.addView(t1);
        	row.addView(t2);
        	row.addView(t3);
        	user_home.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  	
        	}
        	
         	}
        catch (Exception e) 
        {
                    
                    
            }

        
	}
}